<template>
  <div class="header-query-li">
    <div class="header-label" :style="mrzts">{{ query.label+'：' }}</div>
    <div
      class="header-label-span"
      :class="active === null ? 'active': ''"
      :style="active === null ? titleFont: mrzts"
      @click="callBack"
    >
      <span>全部</span>
    </div>
    <div v-for="(item,index) in dataList" :key="'label-'+index" :class="active === item.value ?'active': ''" class="header-label-span" @click="callBack(item)">
      <span :style="active === item.value ? titleFont: mrzts">{{ item.name }}</span>
    </div>
  </div>
</template>

<script>
import { queryAllServiceParamsLabel } from '@/api/portal/manager/setting/index';
export default {
  name: 'OfficeHallItem',
  props: {
    params: {
      type: Object,
      default() {
        return {};
      }
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      query: {},
      dataList: [],
      active: null,
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      titleFont: {},
      mrzts: {}
    };
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = { backgroundColor: newData.mrlb, opacity: newData.tmd };
        this.titleFont = { color: newData.lmys };
        this.mrzts = { color: newData.fontColor };
        this.activeStyle = { color: newData.xsys, backgroundColor: newData.xzbjs };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    params: {
      handler(newData, oldData) {
        this.active = null;
        this.query = newData;
        this.initData({ code: newData.column });
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initData(data) {
      queryAllServiceParamsLabel(data).then(res => {
        const { data } = res;
        this.dataList = data;
      }).catch(() => {
        this.dataList = [];
      });
    },
    callBack(data) {
      if (undefined !== data && undefined !== data.value && data.value !== null) {
        this.active = data.value;
        this.$emit('call-back', { code: this.query.column, value: data.value });
      } else {
        this.active = null;
        this.$emit('call-back', { code: this.query.column, value: null });
      }
    }
  }
};
</script>

<style scoped>
.header-query-li {
  height: auto;
  width: 100%;
  display: flex;
  flex-flow: row wrap;
}
.header-label {
  width:70px;
  line-height: 30px;
  font-weight: bolder;
}
.header-label-span {
  line-height: 20px;
  padding: 5px 3px;
}
.header-label,.header-label-span {
  height: 30px;
  flex: 0 1 auto;
  font-size: 13px;
  box-sizing: border-box;
}
.header-label-span.active > span {
  display: block;
  width: 100%;
  height: 100%;
  color: #ffffff;
  padding: 0px 5px;
  box-sizing: border-box;
  background-color: #0a84ff;
  border-radius: 3px;
}
</style>
